package com.nio;

import java.io.*;

/**
 * Desc: io读写文件
 * @author: lisha 2018/7/27 11:19
 */
public class WriteFile {

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 10; i++) {
            copy();
        }

    }

    public static void copy() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            // 封装数据源
            fr = new FileReader("E:\\books.zip");
            // 封装目的地
            fw = new FileWriter("E:\\books-copy-io.zip");

            // 每次读一个字符数组
            char[] chs = new char[1024];
            int len = 0;
            long start = System.currentTimeMillis();
            // <span style="font-family: Arial;">fr.read(chs))表示每次读一个字符数组</span>
            while ((len = fr.read(chs)) != -1) {

                // 写入数据. // len是读到数据的实际长度
                fw.write(chs, 0, len);
            }
            long end = System.currentTimeMillis();
            System.out.println("copy用时：" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源，关闭遵循就近原则，fw最近，先关fw
            try {
                if(null != fw){
                    fw.close();
                }
                if(null != fr){
                    fr.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
